//
//  ClassCollectionViewController.m
//  Luck
//
//  Created by 纳里健康 on 2018/5/22.
//  Copyright © 2018年 songK. All rights reserved.
//

#import "LGClassCollectionViewController.h"
#import "BrandCollectionViewCell.h"
#import "LGBrandModel.h"

//测试代码
#import "LGConfirmFormViewController.h"
#import "LGConserveConfirmViewController.h"
#import "LGProductDetailViewController.h"

@interface LGClassCollectionViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic ,strong)UICollectionView *collectionView;

@property (nonatomic, strong)NSMutableArray *brandArray;

@end

@implementation LGClassCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.brandArray = [NSMutableArray array];
    
    [self makeCollectionView];
    
    [self brandRequest];
    
}

- (void)brandRequest
{
    [[MainApi sharedInstance] getPath:@"Index/brandList" params:nil resultBlock:^(NSDictionary *responseObject, NSError *error) {
        NSInteger code = [responseObject[@"code"] integerValue];
        
        if(code == 1)
        {
            NSArray *dataArray = responseObject[@"data"];
            
            for (NSDictionary *dict in dataArray) {
                
                LGBrandModel *brandModel = [LGBrandModel yy_modelWithJSON:dict];
                
                [self.brandArray addObject:brandModel];
            }
            
            [self.collectionView reloadData];
        }
    }];
    
    
}


- (void)makeCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //设置滑动方向
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    flowLayout.minimumLineSpacing = 5;
    
    flowLayout.minimumInteritemSpacing = 5;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATOR_HEIGHT - TABBAR_HEIGHT) collectionViewLayout:flowLayout];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    self.collectionView.delegate = self;
    
    self.collectionView.dataSource = self;
    
    self.collectionView.scrollEnabled = YES;
    
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"BrandCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"BrandCollectionViewCell"];
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.brandArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"BrandCollectionViewCell";
    
    BrandCollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    
    [cell setBrandCellWithModel:self.brandArray[indexPath.row]];
    
    return  cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat itemWidth = ceilf(self.collectionView.width - 5 * 5)/4;
    
    return  CGSizeMake(itemWidth, itemWidth + 10);
}

#pragma mark  定义整个CollectionViewCell的四个边距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);//（上、左、下、右）
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    LGConfirmFormViewController *vc = [LGConfirmFormViewController new];

    
//    LGConserveConfirmViewController *vc = [LGConserveConfirmViewController new];
    LGProductDetailViewController *vc = [LGProductDetailViewController new];
    
        [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
